package cn.erp.app.init;

import cn.erp.common.model._MappingKit;

import com.alibaba.druid.filter.stat.StatFilter;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.ext.plugin.config.ConfigKit;
import com.jfinal.ext.plugin.config.ConfigPlugin;
import com.jfinal.ext.plugin.shiro.ShiroInterceptor;
import com.jfinal.ext.plugin.shiro.ShiroPlugin;
import com.jfinal.ext.route.AutoBindRoutes;
import com.jfinal.interceptor.MenusInterceptor;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.OrderedFieldContainerFactory;
import com.jfinal.plugin.druid.DruidPlugin;

public class JfinalConfig extends JFinalConfig {
	Routes routes;

	public static void main(String[] args) {
		JFinal.start("WebContent", 80, "/", 5);
		// JFinal.start("WebRoot", 80, "/");
	}

	@Override
	public void configConstant(Constants me) {
		// TODO Auto-generated method stub

		loadPropertyFile("init.properties");

		ConfigPlugin cfg = new ConfigPlugin();
		ConfigPlugin.setSuffix("properties");
		cfg.addResource(".*.properties");
		cfg.start();

		me.setDevMode(Boolean.parseBoolean(ConfigKit.getStr("devMode")));
		me.setBaseViewPath("WEB-INF/views");
	}

	@Override
	public void configRoute(Routes me) {
		this.routes = me;
		AutoBindRoutes abr = new AutoBindRoutes();
		abr.autoScan(false);
		me.add(abr);
	}

	@Override
	public void configPlugin(Plugins me) {
		// TODO Auto-generated method stub
		DruidPlugin druid = new DruidPlugin(ConfigKit.getStr("jdbc.url"),
				ConfigKit.getStr("jdbc.username"),
				ConfigKit.getStr("jdbc.password"),
				ConfigKit.getStr("jdbc.driverClassName"));
		druid.set(3, 3, 10);
		druid.addFilter(new StatFilter());
		me.add(druid);

		ActiveRecordPlugin arp = new ActiveRecordPlugin(druid);
		arp.setContainerFactory(new OrderedFieldContainerFactory());
		// 展示执行sql
		arp.setShowSql(true);
		me.add(arp);
		_MappingKit.mapping(arp);

		me.add(new ShiroPlugin(routes));
	}

	@Override
	public void configInterceptor(Interceptors me) {
		// TODO Auto-generated method stub

		me.add(new ShiroInterceptor());
		me.add(new MenusInterceptor());

	}

	@Override
	public void configHandler(Handlers me) {
		me.add(new ContextPathHandler("root"));
		// TODO Auto-generated method stub

	}

	//
	public static DruidPlugin createDruidPlugin() {
		return new DruidPlugin(PropKit.get("jdbc.url"),
				PropKit.get("jdbc.username"), PropKit.get("jdbc.password")
						.trim());
	}

}
